#!/bin/bash
#作者：千锋贺洋伟老师-Alan
#未经允许禁止转载与修改作者署名


#【一级菜单】
main_menu(){
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e -n "\e[40;37m \t\t \e[0m" ; echo -e -n "\e[47;34m ***  Linux 自 动 化 脚 本 主 菜 单  ***\e[0m" ; echo -e "\e[40;37m \t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t作者： Alan-贺老师\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【1】- 简 单 优 化 系 统\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【2】- 安 装 服 务\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【3】- 退 出 脚 本\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
}


#【二级菜单】

menu_1(){
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t*** 简 单 优 化 系 统 ***\t\t\t \e[0m" 
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t作者： Alan-贺老师\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【0】安 装 YUM 源\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【1】优 化 ssh 连 接\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【2】永久关闭防火墙、selinux\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【3】设 置 IP 地 址(限NAT)\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【4】修 改 主 机 名(兼容英文大小写)\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【5】返 回 主 菜 单\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
}

menu_2(){
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t*** 服 务 安 装 ***\t\t\t\t \e[0m" 
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t作者： Alan-贺老师\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【1】YUM 安 装 MySQL 5.7\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【2】编 译 安 装 MySQL 5.7\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【3】安装常用软件包（如vim等）\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t【4】返 回 主 菜 单\t\t\t\t \e[0m"
echo -e "\e[40;37m \t\t\t\t\t\t\t\t\t \e[0m"
}

#【函数：优化ssh】
ssh_f(){
	sed -ri "s@GSSAPIAuthentication yes@GSSAPIAuthentication no\nUseDNS=no@" /etc/ssh/sshd_config
	systemctl restart sshd &>/dev/null
	if [ $? -eq 0 ]
	then
		clear
		echo -e "\e[43;34m \t\t\t【SSH配置完成....】\t\t\t\t \e[0m"
	else
		echo -e "\e[41;34m \t\t【请检查你的SSH服务是否正确安装】\t\t \e[0m"
	fi
}

#【函数：关闭防火墙】
firewalld_selinux_no(){
	sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
	setenforce 0 &>/dev/null
	systemctl stop firewalld &>/dev/null
	systemctl disable firewalld &>/dev/null
	clear
	echo -e "\e[43;34m \t\t\t【防火墙与selinux均已经关闭！】\t\t\t \e[0m"
}


#【函数：安装过vim等工具包】
install_vim(){
	yum -y install vim &>/dev/null
	if [ $? -ne 0 ]
	then
		echo "请先安装阿里云YUM源！"
		break
	fi
	rpm -qa | grep vim-common &>/dev/null
	if [ $? -eq 0 ]
	then
		echo "本机已安装vim......"
	else
		echo "正在安装vim，请稍等...."
		yum -y install vim &>/dev/null
		rpm -qa |grep vim-common &>/dev/null
		if [ $? -eq 0 ] 
		then
			echo "......vim安装完毕！"
		else
			echo "请先安装YUM源......"
		fi
	fi

	    rpm -qa |grep bash-completion  &>/dev/null
    if [ $? -eq 0 ] 
    then
        echo "本机已安装自动补全......"
    else 
        echo "正在安装自动补全，请稍等...."
        yum -y install bash-completion &>/dev/null
        rpm -qa |grep bash-completion &>/dev/null
        if [ $? -eq 0 ] 
        then
            echo "......自动补全服务安装完毕！"
        else
            echo "请先安装YUM源......"
        fi
    fi
	yum -y install wget unzip net-tools  lsof  &>/dev/null
	rpm -qa | grep wget &>/dev/null
	if [ $? -eq 0 ] 
        then
            echo "......wget安装完毕！"
        else
            echo "请先安装YUM源......"
        fi
	rpm -qa |grep net-tools &>/dev/null
	if [ $? -eq 0 ] 
        then
            echo "......net-tools安装完毕！"
        else
            echo "请先安装YUM源......"
        fi
}

#【函数：设置IP】
ip_set(){
	clear
	echo -e "\e[47;31m \t\t【请认真填写，否则将会无法连接，谨慎、谨慎、再谨慎.....】\t\t \e[0m"
	read -p "请输入你要指定的网关[返回输入0]： "  gw
	if [ "$gw" == "0" ]
	then
		clear
		continue
	fi
	read -p "请输入你要制定的IP ：  "  my_ip
	echo "正在配置网络.......【请等待1-10秒】"
	cat > /etc/sysconfig/network-scripts/ifcfg-ens33  <<-EOF
	TYPE="Ethernet"
	BOOTPROTO="none"
	DEVICE="ens33"
	ONBOOT="yes"
	IPADDR=$my_ip
	PREFIX=24
	GATEWAY=$gw
	DNS1=114.114.114.114
	NETWORKING=yes
	HOSTNAME=localhost
	EOF
	systemctl stop NetworkManager &>/dev/null
	systemctl restart network &>/dev/null
	if [ $? -eq 0 ]
	then
		clear
		echo -e "\e[43;34m \t\t【IP配置完成！当前IP为：$my_ip】\t\t \e[0m"
	fi

}

#【函数：修改主机名】
hostname_set(){
	clear
	echo -e "\e[47;31m \t\t【请按照要求输入：数字、字母、下划线的组合】\t\t \e[0m"
	read -p "请输入你想设置的主机名：" hostname_str
	echo "$hostname_str" > /etc/hostname
	clear
	echo -e "\e[43;34m \t\t【配置完成！当前主机名为：$my_ip（重启生效）】\t\t \e[0m"
	while :
	do
		read -p "您要重启服务器吗？N/Y :" re
		case "$re" in
		Y|y|yes|Yes|YES)
			clear
			echo -e "\e[43;34m \t\t【......3 秒 后 重 启......】\t\t \e[0m"
			echo -e "\e[43;34m \t\t【......3 秒 后 重 启......】\t\t \e[0m"
			echo -e "\e[43;34m \t\t【......3 秒 后 重 启......】\t\t \e[0m"
			echo -e "\e[43;34m \t\t【......3 秒 后 重 启......】\t\t \e[0m"
			echo -e "\e[43;34m \t\t【......3 秒 后 重 启......】\t\t \e[0m"
			echo -e "\e[43;34m \t\t【......3 秒 后 重 启......】\t\t \e[0m"
			echo -e "\e[43;34m \t\t【......3 秒 后 重 启......】\t\t \e[0m"
			sleep 3
			reboot
		;;
		N|n|NO|No|no)
			clear
			break
		;;
		*)
			clear
			echo "$re"
			echo -e "\e[41;34m \t\t【选 择 错 误 请 重 新 选 择】\t\t \e[0m" 
		;;
		esac
	done
}
#【函数-判断函数】
install_test(){
        if [ $? -eq 0 ]
        then
		echo -e "\e[43;34m \t\t【$1】\t\t \e[0m"
        else
		clear
		echo -e "\e[41;34m \t\t【$2】\t\t \e[0m"	
		break
        fi
}

#【函数-安装与更新阿里源、扩展源】

ali_yum(){
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && yum clean all && yum makecache && curl -o /etc/yum.repos.d/epel.repo  http://mirrors.aliyun.com/repo/epel-7.repo && yum makecache
}

#【函数-安装msyql官方yum源】
yum_mysql(){
        if [ -f "/etc/yum.repos.d/mysql-community.repo" ]
        then
                echo "【你已经安装mysql的YUM源。】"
        else
                while :
                do
			rm -rf /root/mysql80-community-release-el7-3.noarch.rpm
			rm -rf /etc/yum.repos.d/mysql-community*
                        wget dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -P /root
                        if [ $? -eq 0 ]
                        then
                                echo "成功下载mysql源，准备安装YUM源....."
                                rpm -ivh /root/mysql80-community-release-el7-3.noarch.rpm
                                if [ $? -eq 0 ]
                                then
					#【更新mysql5.7密钥信息】
					rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
					#或者【取消密钥验证】
					# sed -r -i '22c\gpgcheck=1'  /etc/yum.repos.d/mysql-community.repo
                                        echo "【成功安装msyql源】"
                                        break
                                else
                                        echo "安装mysql源失败...."
                                fi
                        else
                                echo "下载mysql源失败，将再次尝试下载（可能网络不太通畅哦）...."
                                sleep 2
                        fi
                done
        fi
}

#【函数-mysql预装换进环境】
clear_mysql(){
	/etc/init.d/mysqld stop &>/dev/null
	systemctl stop mysqld &>/dev/null
	ps aux |grep " /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data" | awk '{print $2}' |kill -9 &>/dev/null
	yum remove mysqld &>/dev/null
	yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y &>/dev/null
	userdel -r mysql &>/dev/null
	rm -rf /etc/my* &>/dev/null
	rm -rf /var/lib/mysql &>/dev/null
	rm -rf /usr/local/mysql &>/dev/null
	rpm -e `rpm -qa |grep mysql` &>/dev/null
	rm -rf /var/log/mysql.log &>/dev/null
	rm -rf /var/log/mysqld.log &>/dev/null
	rm -rf /usr/local/mysql &>/dev/null
	rm -rf /usr/local/mysql-5.7.27/CMakeCache.txt &>/dev/null
	rm -rf /usr/bin/mysql
	rm -rf /usr/bin/mysqld
}

#【函数-测试网路】
ping_1(){
	ping -c1 -W1 www.baidu.com  &>/dev/null
	if [ $? -eq 0 ]
	then
		echo "【网络可用】--OK"
	else
		echo "【请检测你的网络以及DNS可用性。】"
		break
	fi
}

#【函数-下载mysql编译安装包】
wget_mysql(){
while :
do
	if [ -f "/root/mysql-boost-5.7.27.tar.gz" ]
	then
		break
	else
		wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz
		if [ $? -eq 0 ]
		then
			break
		fi
	fi
done
}


#【函数-编译安装mysql】
mysql_make_install(){
	rm -rf /usr/local/mysql-5.7.27/CMakeCache.txt
	cd /usr/local/mysql-5.7.27/
	cmake . \
	-DWITH_BOOST=boost/boost_1_59_0/ \
	-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
	-DSYSCONFDIR=/etc \
	-DMYSQL_DATADIR=/usr/local/mysql/data \
	-DINSTALL_MANDIR=/usr/share/man \
	-DMYSQL_TCP_PORT=3306 \
	-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
	-DDEFAULT_CHARSET=utf8 \
	-DEXTRA_CHARSETS=all \
	-DDEFAULT_COLLATION=utf8_general_ci \
	-DWITH_READLINE=1 \
	-DWITH_SSL=system \
	-DWITH_EMBEDDED_SERVER=1 \
	-DENABLED_LOCAL_INFILE=1 \
	-DWITH_INNOBASE_STORAGE_ENGINE=1
	if [ $? -eq 0 ]
	then
		cd /usr/local/mysql-5.7.27/ && make -j 4  && make install
	else
		echo -e "\e[41;34m \t\t【'*******mysql配置失败。。。[即将退出]******'】\t\t \e[0m"
		exit
	fi
}


############################【开始】################################
while :
do
	main_menu
	read -p "请输入你的选择：【 1 / 2 / 3 】 " man_num
	clear
	
	if [ $man_num -eq 1 ]
	then
	#echo -e "\e[45;37m \t\t\t【 下 方 是 二 级 菜 单 】\t\t\t \e[0m"

		while :
		do
			echo -e "\e[45;37m \t\t\t【 下 方 是 二 级 菜 单 】\t\t\t \e[0m"
			menu_1
			read -p "请输入你的选择：【 0 / 1 / 2 / 3 / 4 / 5 】 " num_1
			case $num_1 in
			0)
				ali_yum
			;;
			1)
				ssh_f
			;;
			2)
				firewalld_selinux_no
			;;
			3)
				ip_set
				
			;;
			4)
				hostname_set
				
			;;
			5)
				clear
				break
			;;
			*)
				clear
				echo -e "\e[41;34m \t\t请按照提示输入正确的选项：【 例 如 数 字：1 】\t\t \e[0m"
			;;
			esac
		done
	elif [ $man_num -eq 2 ]
	then
		while :
		do
			echo -e "\e[45;37m \t\t\t【 下 方 是 二 级 菜 单 】\t\t\t \e[0m"
			menu_2	
			read -p "请输入你的选择：【 1 / 2 / 3 】 " num_2
			case $num_2 in
			1)
				#【yum安装流程如下】
				echo -e "准备更新YUM源......\n\t\t（此过程跟自身网络有关）大概需等待1-10分钟。\n\n\n【切勿手动停止或关闭】）"
				sleep 2
				#【清理环境】
				clear_mysql
				
				#【msyql源】
				yum_mysql
				
				
				#【修改MySQL源默认安装版本】
				yum -y install yum-utils
				install_test "下载yum-utils成功" "下载yum-utils失败"
				yum-config-manager --enable mysql57-community
				yum-config-manager --disable mysql80-community
				
				#sed -r -i  -e '21c\enabled=1'  -e '28c\enabled=0'  /etc/yum.repos.d/mysql-community.repo
				 echo -e "【MySQL-YUM源已更换为5.7版本。】\n【即将安装msyql5.7...】"
				 sleep 2
				
				 # 【开始安装MySQL5.7】
				yum install -y mysql-community-server
				install_test	"安装mysql-community-server成功"   "安装mysql-community-server失败"
				# 【启动并开机启动MySQL】
				systemctl start mysqld
				install_test    "启动MySQL成功"  "启动MySQL失败"
				systemctl enable mysqld
				install_test    "设置MySQL开机启动成功" "设置MySQL开机启动失败"
				
				#【查找初始化密码】
				#grep "A temporary password is generated"  /var/log/mysqld.log | awk '{print $NF}' > /root/mysql_init_pw.txt
				 mysql_init_pw=`grep "A temporary password is generated"  /var/log/mysqld.log | awk '{print $NF}'`
				
				
				 #【修改mysql密码为1】
				 echo "validate_password=off" >> /etc/my.cnf
				 systemctl restart mysqld
        		         install_test    "设置mysql配置文件后重启MySQL成功" "设置mysql配置文件后重启MySQL失败"
				 mysqladmin -u root -p"$mysql_init_pw" password "1"
				 sed -r -i '/validate_password=off/d'  /etc/my.cnf
				 systemctl restart mysqld
        		         install_test    "设置MySQL密码为1后重启MySQL成功" "设置MySQL密码为1后重启MySQL失败"
				 rm -rf /root/mysql80-community-release-el7-3.noarch.rpm
				 echo -e "\e[43;34m \t\t【安装完成,3秒后弹出二级菜单界面】\t\t \e[0m"
				 clear
				 echo -e "\e[43;34m \t\t【恭喜YUM安装MySQL成功。且MySQL的root密码已设置为1】\t\t \e[0m"
			;;
			2)
				clear
				echo "准备更新YUM源与下载mysql5.7源码安装包......\n\t\t（此过程跟自身网速有关，需等待5-20分钟。\n\n\n【切勿手动停止或关闭】）"
				sleep 3

				#【清理环境】
				clear_mysql
				
				#【下载安装yum源与5.7源码包】
				wget_mysql
				
				#【准备编译环境】
				yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake ||exit

				mkdir -p /usr/local/mysql/{data,log}
				
				#【解压源码包】
				if [ ! -d /usr/local/mysql-5.7.27 ]
				then	
				tar xzf /root/mysql-boost-5.7.27.tar.gz -C /usr/local/
				fi
				cd /usr/local/mysql-5.7.27/
				userdel -r mysql
				useradd mysql -M -s /sbin/nologin
				#【编译安装】
				mysql_make_install
				
				#【执行初始化】
				chown -R mysql.mysql /usr/local/mysql
				
				/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data  &>/root/mysql_init.txt
				mysql_init_pw=`grep "A temporary password is generated"  /root/mysql_init.txt | awk '{print $NF}'`
				
				
				#【修改初始密码为1】
				cat>/etc/my.cnf<<-alan
				[client]
				port = 3306
				socket = /tmp/mysql.sock
				default-character-set = utf8
				[mysqld]
				port = 3306
				user = mysql
				basedir = /usr/local/mysql
				datadir = /usr/local/mysql/data
				socket = /tmp/mysql.sock
				character_set_server = utf8
				alan
				
				
				#【添加环境变量，简化启动与使用流程】
				echo "PATH=$PATH:$HOME/bin:/usr/local/mysql/bin" >>/etc/profile && source /etc/profile
				cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
				chkconfig --add mysqld
				sleep 2
				while :
				do
					/etc/init.d/mysqld start 
					if [ $? -eq 0 ]
					then
						echo "【启动MySQL成功，即将修改初识密码为：1 】"
						break
					else
						pkill -9 mysqld
						sleep 2
					fi
				done
				/usr/local/mysql/bin/mysqladmin -u root -p"$mysql_init_pw"  password '1'
				chkconfig mysqld on
				ln -s /usr/local/mysql/bin/mysql  /usr/bin/mysql
				sleep 3
				clear
				 echo -e "\e[43;34m \t\t【启动：/etc/init.d/mysqld start 】\t\t \e[0m"
				 echo -e "\e[43;34m \t\t【关闭：/etc/init.d/mysqld stop 】\t\t \e[0m"
				 echo -e "\e[43;34m \t\t【登陆：mysql -u账号 -p密码】\t\t \e[0m"
			;;
			
			3)
				yum -y install vim &>/dev/null
				install_test  "已成功安装vim"   "请先安装YUM源！"
				rpm -qa | grep vim-common &>/dev/null
				if [ $? -eq 0 ]
				then
					echo -e "\e[43;34m \t\t【本机已安装vim......】\t\t \e[0m"
				else
					echo "正在安装vim，请稍等...."
					yum -y install vim &>/dev/null
					rpm -qa |grep vim-common &>/dev/null
					install_test "......vim安装完毕！"  "请先安装YUM源......"
				fi
			
				rpm -qa |grep bash-completion  &>/dev/null
				if [ $? -eq 0 ] 
				then
				    echo -e "\e[43;34m \t\t【本机已安装自动补全......】\t\t \e[0m"
				else 
				    echo "正在安装自动补全，请稍等...."
				    yum -y install bash-completion &>/dev/null
				    rpm -qa |grep bash-completion &>/dev/null
				    install_test "......自动补全服务安装完毕！"  "请先安装YUM源......"
				fi
				yum -y install wget unzip &>/dev/null
				install_test "......wget安装完毕！"  "请先安装YUM源......"
				sleep 2
				clear
				echo -e "\e[43;34m \t\t【常 用 工 具 包 已 经 安 装 完 毕】\t\t\t \e[0m"
			;;
			4)
				clear
				break
			;;
			*)
				clear
				echo -e "\e[41;34m \t\t请按照提示输入正确的选项：【 例 如 数 字：1 】\t\t \e[0m"

			;;
			esac
		done
	elif [ $man_num -eq 3 ]
	then
		exit 0
	else 
		clear 
		echo -e "\e[41;34m \t\t请按照提示输入正确的选项：【 例 如 数 字：1 】\t\t \e[0m"
		
	fi

done


